package com.qfedu.service.impl;

import com.github.pagehelper.PageInfo;
import com.qfedu.dao.FunctionDao;
import com.qfedu.dao.FunctionwithgroupDao;
import com.qfedu.dao.UsergroupDao;
import com.qfedu.entity.Function;
import com.qfedu.entity.Functionwithgroup;
import com.qfedu.entity.Usergroup;
import com.qfedu.service.UsergroupService;
import com.qfedu.util.Result;
import com.qfedu.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * projectName: logistics
 * author: CGY
 * time: 2020/10/9 20:55
 * description:
 */
@Service
public class UsergroupServiceImpl implements UsergroupService {
    @Autowired
    private UsergroupDao usergroupDao;

    @Autowired
    private FunctionDao functionDao;

    @Autowired
    private FunctionwithgroupDao functionwithgroupDao;
    @Override
    public Result selectAll() {
        List<Usergroup> list = usergroupDao.findAll();
        return Result.success(list);
    }

    @Override
    public PageInfo selectAllGroup() {
        List<Usergroup> list = usergroupDao.findAll();
        return new PageInfo<>(list);
    }

    @Override
    public Result addGroup(Usergroup usergroup) {
        Usergroup group = usergroupDao.save(usergroup);
        return ResultUtil.getObject(group,"添加失败");
    }

    @Override
    public Result selectGroupById(Integer groupId) {

        Usergroup user = usergroupDao.findUsergroupById(groupId.longValue());
        return ResultUtil.getObject(user,"查询失败");
    }

    @Override
    public Result updateDescription(Usergroup usergroup) {
        return ResultUtil.getObject(usergroupDao.save(usergroup),"更新失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteGroup(Long id) {
        Usergroup user = usergroupDao.findUsergroupById(id);
        if(user == null){
            return Result.error("用户不存在");
        }
        Integer row = usergroupDao.deleteUsergroupById(id);
        return ResultUtil.getObject(row,"删除失败");
    }

    @Override
    public Result selectAllFunction() {
        List<Function> list = functionDao.findAll();

        return ResultUtil.getObject(list,"查询失败");
    }

    @Override
    public Result selectFunctionByGroup(Long groupId) {
        List<Functionwithgroup> list = functionwithgroupDao.findAllByGroupId(groupId);
        return ResultUtil.getObject(list,"查询失败");
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addNewFunc(Long groupId, Integer[] array) {
        List<Functionwithgroup> lists = new ArrayList<>();
        if(array != null || array.length>0){
            for (Integer func : array) {
                Functionwithgroup funcGroup = new Functionwithgroup();
                funcGroup.setGroupId(Math.toIntExact(groupId));
                funcGroup.setFunctionId((int) func.longValue());
                lists.add(funcGroup);
            }
        }else{
            return Result.error("权限不能为空");
        }
        //先清空权限
        functionwithgroupDao.deleteByGroupId(groupId);

        for (Functionwithgroup group : lists) {
            functionwithgroupDao.save(group);
        }
        return ResultUtil.getObject(lists,"保存失败");
    }
}
